package com.xneure.code.msg;

import java.lang.reflect.Method;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
import com.xneure.utils.ObjectUtil;

@SuppressWarnings("rawtypes")
@Component
public class MsgPostProcessor implements BeanPostProcessor{

	@SuppressWarnings("unchecked")
	@Override
	public Object postProcessBeforeInitialization(Object bean,String beanName) throws BeansException{
		Method[] metd = bean.getClass().getDeclaredMethods();
		if(null != metd && metd.length > 0){
			for(Method m:metd){
				MsgListener ant = m.getAnnotation(MsgListener.class);
				if(null != ant){
					try{
						MessageListener rs = (MessageListener)ObjectUtil.callMethod(bean,m);
						if(Void.class.equals(ant.value())){
							MsgManage.getSysMsgManage().registerListen(rs);
						}else{
							MsgManage.getSysMsgManage().registerListen(ant.value(),rs);
						}
					}catch(Exception e){
					}
				}
			}
		}
		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean,String beanName) throws BeansException{
		return bean;
	}
}
